package com.bulemobi.activity.Ac_CarService;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.bulemobi.data.DataCentre;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.net.tsz.afinal.core.AsyncTask;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.util.MD5Util;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by 武 on 2014/9/23 0023.
 * 预约等待
 */
public class Ac_Appointment extends BaseActivity {
    @ViewInject(id = R.id.tv_confirm)
    TextView tv_confirm;
    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;
    @ViewInject(id = R.id.tv_time)
    TextView tv_time;

    private int time = 10;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            time--;
            tv_time.setText(time + "");
            if (time > 0) {
                handler.postDelayed(this, 1000);
            } else {
                cancleOrder();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_appointment_main);
        initView();
    }

    private void initView() {
        actionBarView.button_back.setVisibility(View.GONE);
        actionBarView.title.setText("预约等待");
        handler.postDelayed(runnable, 1000);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancleOrder();
                handler.removeCallbacks(runnable);
            }
        });
    }

    String stri;
    byte[] bytes;
    HttpPost httpPost;
    HttpClient client;
    String result;

    private void cancleOrder() {
        httpPost = new HttpPost(UrlCentre.ADJ_BASEURL);
        String latLngMD5 = "";
        try {
            latLngMD5 = MD5Util.encodeByMD5(DataCentre.orderNum + "aidaijia");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        stri = "1209|" + DataCentre.orderNum + "|3fe5d0205970e8a732686c7cc2e74745|" + latLngMD5;
        Log.d("Ac_replace", stri);
        bytes = new byte[0];
        client = new DefaultHttpClient();
        AsyncTask task = new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {
                try {
                    bytes = stri.getBytes("UTF-8");
                    ByteArrayInputStream bs = new ByteArrayInputStream(bytes, 0, bytes.length);
                    HttpEntity entity = new InputStreamEntity(bs, bytes.length);
                    httpPost.setEntity(entity);
                    HttpResponse response = client.execute(httpPost);
                    result = EntityUtils.toString(response.getEntity(), "UTF-8");
                    Log.d("DOINBACKGROUND", result);
                    bs.close();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {

            }
        };
        task.execute();
    }

}
